# -*- coding: utf-8 -*-
# @Time : 2023/4/27 14:08:08
# @Author : sacon
# @File : 2.下载.py
import base64
import hashlib
import json
import math
import os
import random
import shutil
import time
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import datetime

import pandas
import re
import pymongo
from loguru import logger
import requests
from requests.auth import HTTPBasicAuth


BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DOWN_DIR = os.path.join(BASE_DIR, 'Cache')
print(DOWN_DIR)
import redis

DB_HOST = '192.168.44.53'
DB_PORT = 6379
DB_PASSWORD = 'vQLzqNlGA4z5hZBS'
Pool = redis.ConnectionPool(host=DB_HOST, port=6379, password=DB_PASSWORD, max_connections=10, db=6, socket_keepalive=True, socket_connect_timeout=5)
Redis_Con = redis.Redis(connection_pool=Pool)

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
}
file_list = ["pdf", "caj", "txt", "epub", "octet-stream"]

ES_URL = 'http://140.143.51.14:9200'
ES_Author = ("robot", "ko0cXhepXD3FOLtq")


def ESSearch(con, Type="Doi"):
    if not con:
        logger.error("请检查检索内容不得为空！")
        return []
    headers = {
        'Connection': 'close'
    }
    url = f"{ES_URL}/wwzy_journal_article_20240705,reading_room_article_v3,cma_20240621,oss-pdf,oss-pdf2,newmed-20230221/_search"
    if Type == "Doi":
        query_json = {
            "query": {
                "bool": {
                    "should": [
                        {
                            "term": {
                                "article_doi": str(con)
                            }
                        },
                        {
                            "term": {
                                "doi.keyword": str(con)
                            }
                        }
                    ],
                    "minimum_should_match": 1
                }
            }
        }
    else:
        query_json = {
            "query": {
                "bool": {
                    "should": [
                        {
                            "match": {
                                "title": {
                                    "query": con,
                                    "minimum_should_match": "100%"
                                }
                            }
                        },
                        {
                            "match": {
                                "article_title": {
                                    "query": con,
                                    "minimum_should_match": "100%"
                                }
                            }
                        },
                        {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "ti": {
                                                "query": con,
                                                "minimum_should_match": "100%"
                                            }
                                        }
                                    },
                                    {
                                        "match": {
                                            "docu_type": "期刊"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "size": 100
        }
    response = requests.post(url, json=query_json, auth=ES_Author, headers=headers, timeout=10)
    json_data = response.json()
    hits = json_data["hits"]["hits"]
    if not hits:
        return []
    else:
        result = []
        for x in hits:
            index = x["_index"]
            article_local_address = x["_source"].get("article_local_address", "")
            doi = x["_source"].get("article_doi", "")
            if doi != con:
                continue
            result.append({"source": index, "pdf_url": article_local_address})
        return result


class EnglishLit2_:

    # ES 检索进行封装
    def es_down(self, con, Type="Doi"):
        if Type == "Doi":
            result = ESSearch(con)
        else:
            result = ESSearch(con, "Title")

        result = [x for x in result if x["pdf_url"]]
        if result:
            return result
        return []


EnglishLit2_Obj = EnglishLit2_()
if __name__ == '__main__':

    # doi = "10.5397/cise.2023.00227"
    # print(EnglishLit2_Obj.es_down(doi, ))
    doi = "Epilepsy after meningitis: A systematic review and meta-analysis"
    print(EnglishLit2_Obj.es_down(doi, "D"))
